############## # Program #1 # ############## import random L = [ 0 ] * 19 n = int(input("Enter n: ")) i = 0 while i < n: x = random.randint(1, 6) y = random.randint(1, 6) z = random.randint(1, 6) sum = x + y + z L[sum] += 1 i += 1 for i in range(0, 19): L[i] /= n for i in range(0, 19): print("{0:6d} {1:6.4f}".format(i, L[i])) ############## # Program #2 # ############## low = 1 high = 100000000 n = low outFile = open("output.txt", "w") while n <= high: L = [] divisor = 1 while divisor * divisor <= n: if divisor * divisor == n: L.append(divisor) elif n % divisor == 0: L.append(divisor) L.append(int(n / divisor)) divisor += 1 if len(L) >= 300: L.sort() outFile.write(str(n) + " " + str(len(L)) + " " + str(sum(L)) + " ") for i in L: outFile.write(str(i) + " ") outFile.write("\n") n += 1 outFile.close() ############## # Program #3 # ############## import sys import random import time import math # s and t are squares. # Here, we need to use x/y/radius, i.e. [0], [1], [2] indices. def overlap(s, t): sUpperLeftX = s[0] - s[2] sUpperLeftY = s[1] + s[2] tUpperLeftX = t[0] - t[2] tUpperLeftY = t[1] + t[2] sLowerRightX = s[0] + s[2] sLowerRightY = s[1] - s[2] tLowerRightX = t[0] + t[2] tLowerRightY = t[1] - t[2] if sUpperLeftX > tLowerRightX or tUpperLeftX > sLowerRightX or \ sUpperLeftY < tLowerRightY or tUpperLeftY < sLowerRightY: return False else: return True # ----------------------------------------------------------------- # Main program random.seed() numSquares = 10 totalInitialLittleSquareArea = .001 side = math.sqrt(totalInitialLittleSquareArea / numSquares) # Create a list of squares. square = [] for i in range(0, numSquares): # Pick a point on the board. It's possible that some of the little # square may lie outside the board, but center will not. # random.random() returns 0.0 <= x < 1.0 which is perfect for me. x = random.random() y = random.random() radius = side / 2.0 square.append([x, y, radius, False]) iters = 0 begin = time.clock() while True: iters += 1 numberNonoverlapping = 0 for s in square: if s[3] == True: continue foundOverlap = False for t in square: if s != t and overlap(s, t): foundOverlap = True break if foundOverlap: s[3] = True else: s[2] *= 1.01 numberNonoverlapping += 1 print(numberNonoverlapping) if numberNonoverlapping == 0: break end = time.clock() duration = end - begin print("{0:4d} squares, {1:5d} iterations, {2:6.2f} seconds". \ format(numSquares, iters, duration)) ############## # Program #4 # ############## L = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,25,50,75,100] selection = [] ans = [] for a in range(0, 24): for b in range(a+1, 24): for c in range(b+1, 24): for d in range(c+1, 24): for e in range(d+1, 24): for f in range(e+1, 24): selection.append([L[a],L[b],L[c],L[d],L[e],L[f]]) ans.append(selection[0]) for i in range(1, len(selection)): print("Trying selection # ", i) matchFound = False for j in range(0, len(ans)): if selection[i][0] == ans[j][0] and \ selection[i][1] == ans[j][1] and \ selection[i][2] == ans[j][2] and \ selection[i][3] == ans[j][3] and \ selection[i][4] == ans[j][4] and \ selection[i][5] == ans[j][5]: matchFound = True if not matchFound: ans.append(selection[i]) print(" Answer now has ", len(ans)) outFile = open("numbers.txt", "w") for i in ans: for j in i: outFile.write(str(j) + " ") outFile.write("\n") outFile.close() ############## # Program #5 # ############## input = input("Please enter a word: ") capital = False lowercase = False digit = False symbol = False for letter in input: if letter >= 'A' and letter <= 'Z': capital = True if letter >= 'a' and letter <= 'z': lowercase = True if letter >= '0' and letter <= '9': digit = True if letter in "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/": symbol = True if capital and lowercase and digit and symbol: print ("Good\n") else: print ("Not so good\n") points = 0 if capital == True: points += 1 if lowercase == True: points += 1 if digit == True: points += 1 if symbol == True: points += 1 print ("You have", points, "points.") ############## # Program #6 # ############## text = input("Enter some text: ") z = 5 - len(text) % 5 if z == 5: z = 0 for i in range(z): text += "z" text2 = "" for f in range(5): for i in range(len(text)): if i % 5 == f: text2 += text[i] print("Answer is: ", text2) ############## # Program #7 # ############## alphabet = "abcdefghijklmnopqrstuvwxyz" text = input("Enter some text: ") word = input("Enter key: ") answer = "" for index, letter in enumerate(text): number = alphabet.index(letter) + 1 wordNumber = alphabet.index(word[index % len(word)]) + 1 value = (number + wordNumber - 1) % 26 letter = alphabet[value] answer += letter print(letter, "+", word[index % len(word)], "=", letter) print("Answer is: ", answer) ############## # Program #8 # ############## def calc_letter_dist(s): alphabet = "abcdefghijklmnopqrstuvwxyz" s = s.lower() L = [0,0,0,0,0, 0,0,0,0,0, \ 0,0,0,0,0, 0,0,0,0,0, \ 0,0,0,0,0, 0] for c in s: index = alphabet.find(c) print ("index = ", index) # Why do we need this if statement? if index != -1: L[index] += 1 print (L) return L # ------------------------------------------------------------ s1 = input("Enter first string: ") s2 = input("Enter second string: ") L1 = calc_letter_dist(s1) L2 = calc_letter_dist(s2) if L1 == L2: print("yes") else: print("no") ############## # Program #9 # ############## def calc_letter_dist(s): alphabet = "abcdefghijklmnopqrstuvwxyz" s = s.lower() L = [0,0,0,0,0, 0,0,0,0,0, \ 0,0,0,0,0, 0,0,0,0,0, \ 0,0,0,0,0, 0] for c in s: index = alphabet.find(c) #print ("index = ", index) if index != -1: L[index] += 1 return L def anagram2(s1, s2): L1 = calc_letter_dist(s1) L2 = calc_letter_dist(s2) if L1 == L2: return True else: return False # ---------------------------------------------------------- inFile = open("words2.txt", "r") nines = [] for line in inFile: line = line[:-1] if len(line) == 9: nines.append(line) inFile.close() print("I found", len(nines)) L1 = [] L2 = [] for word1 in nines: print(word1) found = False for word2 in nines: if word1 == word2: continue if anagram2(word1, word2): L1.append(word1) found = True if found == False: L2.append(word1) print(len(L1)) print(len(L2)) a_file = open("a9.txt", "w") for word in L1: a_file.write(word + "\n") a_file.close() na_file = open("na9.txt", "w") for word in L2: na_file.write(word + "\n") na_file.close() ############### # Program #10 # ############### def calc_letter_dist(s): alphabet = "abcdefghijklmnopqrstuvwxyz" s = s.lower() L = [0,0,0,0,0, 0,0,0,0,0, \ 0,0,0,0,0, 0,0,0,0,0, \ 0,0,0,0,0, 0] for c in s: index = alphabet.find(c) if index != -1: L[index] += 1 #print (L) return L def anagram2(s1, s2): L1 = calc_letter_dist(s1) L2 = calc_letter_dist(s2) if L1 == L2: return True else: return False #---------------------------------------------------------------- word_list = [ [], [], [], [], [], [], [], [], [], [] ] inFile = open("words2.txt", "r") for line in inFile: line = line[:-1] if len(line) < 1 or len(line) > 8: continue word_list[len(line)].append(line) inFile.close() for i in range(1, 9): print(i, len(word_list[i])) inFile = open("na.txt", "r") for line in inFile: line = line[:-1] word_list[9].append(line) inFile.close() outFile = open("puzzles.txt", "w") for candidate in word_list[9]: print(candidate) for i in range(1, 5): for word1 in word_list[i]: for word2 in word_list[9-i]: phrase = word1 + word2 if anagram2(candidate, phrase): print(word1, word2, "->", candidate) outFile.write(word1 + " " + word2 + " -> " + candidate + "\n") outFile.close()